package com.huike.web.controller.business;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.service.ITbBusinessService;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;

/**
 * 商机跟进记录Controller
 *
 * @date 2021-04-28
 */
@RestController
@RequestMapping("/business/record")
public class TbBusinessTrackRecordController extends BaseController {

    @Autowired
    private ITbBusinessTrackRecordService tbBusinessTrackRecordService;
    @Autowired
    private ITbBusinessService tbBusinessService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询商机跟进记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:record:list')")
    @GetMapping("/list")
    public AjaxResult list(@RequestParam("businessId") Long id) {
        List<TbBusinessTrackRecord> tbBusinessTrackRecords = tbBusinessTrackRecordService.selectByBusinessId(id);
//        替换business中的key_items是数字的情况
        for (TbBusinessTrackRecord tbBusinessTrackRecord : tbBusinessTrackRecords) {
            String keyItems = tbBusinessTrackRecord.getKeyItems();
            String[] split = keyItems.split(",");
            for (int i = 0; i < split.length; i++) {
                String key_item = sysDictDataService.selectDictLabel("communication_point", split[i]);
                tbBusinessTrackRecord.getKeys().add(key_item);
            }
        }
        return AjaxResult.success(tbBusinessTrackRecords);
    }

    /**
     * 新增商机跟进记录
     */
    @PreAuthorize("@ss.hasPermi('business:record:add')")
    @Log(title = "商机跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusinessTrackVo businessTrackVo) {
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        TbBusiness tbBusiness = new TbBusiness();
//        给tbBusiness设置id字段的值
        tbBusiness.setId(businessTrackVo.getBusinessId());
//        改变business中商机的状态，跟进中状态为“2”
        tbBusiness.setStatus("2");
        BeanUtils.copyProperties(businessTrackVo, tbBusiness);
        BeanUtils.copyProperties(businessTrackVo, tbBusinessTrackRecord);
//        先保存商机跟进表
        tbBusinessTrackRecordService.addBusinessTrackRecord(tbBusinessTrackRecord);
//        在更新商机表中新增的用户信息
        tbBusinessService.updateTbBusiness(tbBusiness);
        return AjaxResult.success("商机跟进成功");
    }
}